package com.gton.io;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * @description: 服务间的RPC调用公共请求参数模板
 * 充血型模型
 * @author: GuoTong
 * @createTime: 2022-10-22 16:02
 * @since JDK 1.8 OR 11
 **/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class RPCCommonRequest implements Serializable {
    /**
     * Description: Dubbo链路追踪唯一ID
     *
     * @author: GuoTong
     * @date: 2022-10-19 20:23:36
     */
    public String traceId;

    /**
     * Description: RPC 的发起方
     *
     * @author: GuoTong
     * @date: 2022-10-19 20:25:37
     */
    public String formServer;


    /**
     * Description: RPC 的接收方
     *
     * @author: GuoTong
     * @date: 2022-10-19 20:25:37
     */
    public String toServer;

    /**
     * Description: 开启发送时间
     *
     * @author: GuoTong
     * @date: 2022-10-19 20:26:58
     */
    public LocalDateTime beginTime;

    /**
     * Description: 版本
     *
     * @author: GuoTong
     * @date: 2022-10-19 20:27:23
     */
    public String version;


    /**
     * Description:用户Session
     *
     * @author: GuoTong
     * @date: 2022-10-19 20:29:07
     */
    public String userSessionKey;




    /**
     * Description: 充血型模型,根据“输入比较版本”和“比较条件”
     *
     * @param minVersion
     * @author: GuoTong
     * @date: 2022-10-22 16:17:52
     * @return:boolean
     */
    public boolean compareVersionByFlag(String minVersion, String flag) {
        if (StringUtils.isEmpty(this.version)) {
            this.version = "1.0.0";
        }
        /*
        flag取值{gt=大于，lt=小雨，eq =等于，没传=大于等于}
         */
        switch (flag) {
            case "gt":
                return compareVersion(this.version, minVersion) > 0;
            case "lt":
                return compareVersion(this.version, minVersion) < 0;
            case "eq":
                return compareVersion(this.version, minVersion) == 0;
            default:
                return compareVersion(this.version, minVersion) >= 0;
        }
    }




    public int compareVersion(String version1, String version2) {
        if (version1.equals(version2)) {
            return 0;
        }
        String[] version1Array = version1.split("\\.");
        String[] version2Array = version2.split("\\.");
        int index = 0;
        //获取最小长度值
        int minLen = Math.min(version1Array.length, version2Array.length);
        int diff = 0;
        //循环判断每位的大小
        while (index < minLen && (diff = Integer.parseInt(version1Array[index]) - Integer.parseInt(version2Array[index])) == 0) {
            index++;
        }
        if (diff == 0) {
            //如果位数不一致，比较多余位数
            for (int i = index; i < version1Array.length; i++) {
                if (Integer.parseInt(version1Array[i]) > 0) {
                    return 1;
                }
            }

            for (int i = index; i < version2Array.length; i++) {
                if (Integer.parseInt(version2Array[i]) > 0) {
                    return -1;
                }
            }
            return 0;
        } else {
            return diff > 0 ? 1 : -1;
        }
    }

}


